package com.group4.rbac;

import com.group4.rbac.shiro.DBRealm;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public Realm realm(){
        return new DBRealm();
    }
    @Bean
    public DefaultWebSecurityManager securityManager(){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(realm());
        return defaultWebSecurityManager;
    }

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition (){
        DefaultShiroFilterChainDefinition shiro = new DefaultShiroFilterChainDefinition();
        shiro.addPathDefinition("/**","anon");
        return shiro;

    }
//
//    @Bean
//    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        shiroFilterFactoryBean.setLoginUrl("/rbac/user/login");
//        shiroFilterFactoryBean.setUnauthorizedUrl("/rbac/user/login");
//        shiroFilterFactoryBean.setSecurityManager(securityManager());

//        Map<String, String> filters = new HashedMap();
//        filters.put("jwt", new JwtFilter());
//        shiroFilterFactoryBean.setFilters(filters);
//
//        Map<String, String> map = new LinkedHashMap<>();
//        map.put("/rbac/user/login", "anon");
//        map.put("/center/ucenter/**", "authc");
//        map.put("/order/add", "authc");
//        map.put("/member/loginOut", "logout");
//        map.put("/api/**", "jwt,perms[perm_list]");
//        map.put("/**", "anon");
//
//        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
//        return shiroFilterFactoryBean;
//    }
}
